package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Workflow;
import com.qidianit.mes.pro.mapper.WorkFlowMapper;
import com.qidianit.mes.pro.service.WorkFlowService;
import com.qidianit.mes.pro.vo.WorkflowProcessByProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/18 - 13:54
 */
@Service
public class WorkFlowServiceImpl implements WorkFlowService {
    @Autowired
    private WorkFlowMapper workFlowMapper;


    @Override
    public PageInfo<WorkflowProcessByProduct> queryWorkflowProcessByProduct(Integer id, Integer productionId) {
        PageHelper.startPage(1,10);
        List<WorkflowProcessByProduct> workflowProcessByProduct = workFlowMapper.queryWorkflowProcessByProduct(id,productionId);
        PageInfo<WorkflowProcessByProduct> pageInfo = new PageInfo<>(workflowProcessByProduct);
        return pageInfo;
    }

    @Override
    public PageInfo<Workflow> queryWorkFlowListByManyTags(Integer page, Integer size, String workflowCode, String workflowName, Integer status) {
        PageHelper.startPage(page, size);
        List<Workflow> workflowList = workFlowMapper.queryWorkFlowListByManyTags(page,size,workflowCode,workflowName,status);
        PageInfo<Workflow> pageInfo = new PageInfo<>(workflowList);
        return pageInfo;
    }

    @Override
    public Workflow queryWorkFlowByCode(String workflowCode) {
        return workFlowMapper.queryWorkFlowByCode(workflowCode);
    }

    @Override
    public int addWorkFlow(Workflow workflow) {
        Workflow workflow1 = workFlowMapper.queryWorkFlowByCode(workflow.getWorkflowCode());
        if (workflow1 != null) {
            return -1;
        }
        return workFlowMapper.addWorkFlow(workflow);
    }

    @Override
    public int updateWorkFlow(Workflow workflow) {
        Workflow workflow1 = workFlowMapper.queryWorkFlowByCode(workflow.getWorkflowCode());
        if(workflow1.getWorkflowCode().equals(workflow.getWorkflowCode())){
            return workFlowMapper.updateWorkFlow(workflow);
        }
        if (workflow1 != null) {
            return -1;
        }
        return workFlowMapper.updateWorkFlow(workflow);
    }

    @Override
    public int deleteWorkFlow(Integer id) {
        return workFlowMapper.deleteWorkFlow(id);
    }

    @Override
    public int deleteWorkFlowByListIds(List<Integer> ids) {
        Integer sum = 0;
        for (Integer id : ids) {
            if (workFlowMapper.deleteWorkFlow(id) > 0) {
                sum++;
            }
        }
        if (sum.equals(ids.size() )) {
            return 1;
        }else if (sum > 0) {
            return -1;
        }
        return 0;
    }

}
